package com.intelli4.pos.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AuthDialog extends JDialog {
	private static final long serialVersionUID = 3687210096489680865L;

	private boolean authenticated = false;

	private JTextField loginIdFld;
	private NumberPadPanel numberPadPanel;
	private JButton okayBtn;
	private JButton cancelBtn;

	public AuthDialog() {
		super();

		init();
	}

	protected void init() {
		setLayout(new BorderLayout());

		JPanel loginIdPanel = new JPanel(new BorderLayout());
		loginIdPanel.add(new JLabel("Login ID"), BorderLayout.WEST);

		loginIdFld = new JTextField();
		loginIdPanel.add(loginIdFld, BorderLayout.CENTER);

		add(loginIdPanel, BorderLayout.NORTH);

		numberPadPanel = new NumberPadPanel();
		numberPadPanel.addNumberPadListener(new NumberPadListener() {
			@Override
			public void onDecimalPoint() {
			}

			@Override
			public void onDelete() {
				String t = loginIdFld.getText();
				if (t.length() > 0)
					t = t.substring(0, t.length() - 1);
				loginIdFld.setText(t);
			}

			@Override
			public void onNumber(int num) {
				String t = loginIdFld.getText();
				t += String.valueOf(num);
				loginIdFld.setText(t);
			}
		});
		add(numberPadPanel, BorderLayout.CENTER);

		JPanel commandPanel = new JPanel(new FlowLayout());
		okayBtn = new JButton("OK");
		okayBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				authenticated = true;
				dispose();
			}
		});
		commandPanel.add(okayBtn);
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		commandPanel.add(cancelBtn);
		add(commandPanel, BorderLayout.SOUTH);

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public boolean isAuthenticated() {
		return authenticated;
	}

}
